package com.yanqu.road.dataHelper.repair.logic;

import com.yanqu.road.dao.db.DbParameter;
import com.yanqu.road.dataHelper.gameServer.GameServer;
import com.yanqu.road.dataHelper.repair.BaseRepair;
import com.yanqu.road.dataHelper.repair.mgr.UUIDHelper;
import com.yanqu.road.entity.player.UserMail;

import java.math.BigInteger;
import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.*;
import java.util.concurrent.ConcurrentHashMap;

/**
 * @Author wpj
 * @Date 2020/8/19
 */
public class GoldSpeedBuChang extends BaseRepair {

    public GoldSpeedBuChang(GameServer gameServer) {
        super(gameServer);
    }

    @Override
    public void repair(long param1, String param2) {
        Date date = new Date();
        Map<Long, BigInteger> dataMap = getData();
        List<UserMail> userMails = new ArrayList<>();
        for (Map.Entry<Long, BigInteger> entry : dataMap.entrySet()) {
            UserMail userMail = new UserMail();
            userMail.setMailUid(UUIDHelper.saveRandomUUID(getGameServer().getServerInfo().getServerId() + ""));
            userMail.setMailType(0);
            userMail.setTitle("賺速修復補償");
            userMail.setContent("親愛的掌櫃：\n" +
                    "    本次版本更新，對掌櫃的整體賺速進行了大幅度縮放，很抱歉給掌櫃們帶來許多不好的影響和體驗，為此，根據掌櫃更新前的賺速，小二特地為掌櫃準備了豐厚的補償，還請掌櫃笑納，如果掌櫃對本次調整有任何疑問，可聯繫官網客服為您解答，祝掌櫃財運亨通，蒸蒸日上！");
            userMail.setAnnexes(getRewardString(entry.getValue()));
            userMail.setSendTime(date);
            userMail.setSendUserId(0);
            userMail.setSendNickName("系统管理员");
            userMail.setReceiveUserId(Long.valueOf(entry.getKey()));
            userMail.setRead(false);
            userMail.setReadTime(date);
            userMail.setGetReward(false);
            userMail.setDelete(false);
            userMails.add(userMail);
        }
        insertMail(userMails);
    }

    public String getRewardString(BigInteger speed) {
        if (speed.compareTo(BigInteger.valueOf(10000000000l))>= 0) {
            return "104500=1;101000=888;110072=10;110073=10;110011=2;110001=40;110062=500;110000=240;110050=180;110051=180;110049=180;110040=240";
        } else if (speed.compareTo(BigInteger.valueOf(5000000000l)) >= 0  ) {
            return "104500=1;101000=888;110072=8;110073=8;110011=2;110001=35;110062=500;110000=220;110050=160;110051=160;110049=160;110040=220";
        } else if (speed.compareTo(BigInteger.valueOf(1000000000l)) >= 0  ) {
            return "104503=1;101000=888;110072=6;110073=6;110011=2;110001=30;110062=500;110000=200;110050=140;110051=140;110049=140;110040=200";
        } else if (speed.compareTo(BigInteger.valueOf(500000000l)) >= 0  ) {
            return "104502=1;101000=888;110072=5;110073=5;110011=2;110001=25;110062=500;110000=180;110050=120;110051=120;110049=120;110040=180";
        } else if (speed.compareTo(BigInteger.valueOf(100000000l)) >= 0  ) {
            return "104501=1;101000=888;110072=4;110073=4;110011=2;110001=20;110062=500;110000=160;110050=100;110051=100;110049=100;110040=160";
        } else if (speed.compareTo(BigInteger.valueOf(50000000l)) >= 0  ) {
            return "104403=1;101000=888;110072=3;110073=3;110011=2;110001=15;110062=500;110000=140;110050=80;110051=80;110049=80;110040=140";
        } else if (speed.compareTo(BigInteger.valueOf(10000000l)) >= 0  ) {
            return "104402=1;101000=888;110072=2;110073=2;110011=2;110001=10;110062=500;110000=120;110050=60;110051=60;110049=60;110040=120";
        } else {
            return "104400=1;101000=888;110072=1;110073=1;110011=2;110001=5;110062=500;110000=100;110050=40;110051=40;110049=40;110040=100";
        }
    }

    public Map<Long, BigInteger> getData() {
        Map<Long, BigInteger> map = new ConcurrentHashMap<>();
        ResultSet rs = null;
        CallableStatement call = null;
        try {
            call = getGameServer().getConnection().prepareCall("select userId, earnSpeed from t_u_user");
            rs = call.executeQuery();
            while (rs.next()) {
                long userId = rs.getLong("userId");
                BigInteger speed = new BigInteger(rs.getString("earnSpeed"));
                getLogger().error("{}， {}", userId, speed.toString());
                map.put(userId, speed);
            }
        } catch (SQLException e) {
            getGameServer().getLogger().error("异常错误", e);
        } finally {
            getGameServer().closeConnection(call, rs);
            return map;
        }
    }

    public void insertMail(List<UserMail> userMails) {
        String sql = "INSERT INTO t_u_mail(mailUid,mailType,title,content,annexes,sendTime,sendUserId,sendNickName,receiveUserId,isRead,readTime,isGetReward,isDelete) VALUES(?,?,?,?,?,?,?,?,?,?,?,?,?)";

        List<Map<Integer, DbParameter>> mapList = new ArrayList<>();
        try {
            for (UserMail info : userMails) {
                Map<Integer, DbParameter> para = new HashMap<>();
                int i = 1;
                para.put(i++, new DbParameter(Types.NVARCHAR, info.getMailUid()));
                para.put(i++, new DbParameter(Types.INTEGER, info.getMailType()));
                para.put(i++, new DbParameter(Types.NVARCHAR, info.getTitle()));
                para.put(i++, new DbParameter(Types.NVARCHAR, info.getContent()));
                para.put(i++, new DbParameter(Types.VARCHAR, info.getAnnexes()));

                para.put(i++, new DbParameter(Types.TIMESTAMP, info.getSendTime()));
                para.put(i++, new DbParameter(Types.BIGINT, info.getSendUserId()));
                para.put(i++, new DbParameter(Types.VARCHAR, info.getSendNickName()));
                para.put(i++, new DbParameter(Types.BIGINT, info.getReceiveUserId()));

                para.put(i++, new DbParameter(Types.BOOLEAN, info.isRead()));
                para.put(i++, new DbParameter(Types.TIMESTAMP, info.getReadTime()));
                para.put(i++, new DbParameter(Types.BOOLEAN, info.isGetReward()));
                para.put(i++, new DbParameter(Types.BOOLEAN, info.isDelete()));
                mapList.add(para);
            }

            getGameServer().executeBatchQuery(sql, mapList);
        } catch (Exception e) {
            getLogger().error(Thread.currentThread().getStackTrace()[1].getMethodName() + ":", e);
        }
    }
}
